package com.content.view.Component.PurchasedRes
{
	import com.adobe.utils.DateUtil;
	import com.clerkq.CircleList.data.ThumbVVO;
	import com.clerkq.CircleList.view.ThumbV;
	import com.clerkq.Scroller.Scroller;
	import com.clerkq.Scroller.ScrollerType;
	import com.content.model.Collection.ItemGroup;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.model.OnlineRes.OnlineResVO;
	import com.content.view.Component.Button.ImageButton;
	import com.content.view.Component.OnlineRes.OnlineResEvent;
	import com.content.view.Component.OnlineRes.ResItem;
	import com.content.view.Frame.MainPanel.SPBase;
	import com.library.utils.TimeUtil;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class SPMyShelf extends SPBase
	{
		public static const PLAY_RES:String = "SPMyShelf-PlayRes";
		
		private var _resVOGroup:ItemGroup;
		
		private var _container:Sprite;
		private var _shelfMask:Shape;
		private var _resWrapper:Sprite;
		private var _scroller:Scroller;
		
		private var _curResItem:ResItem;
		
		public function SPMyShelf()
		{
			super(this);
			
			_resVOGroup = new ItemGroup();
			
			_container = new Sprite();
			addChild(_container);
			_shelfMask = new Shape();
			_shelfMask.graphics.beginFill(0xff0000, 0.5);
			_shelfMask.graphics.drawRect(0,0,10,10);
			addChild(_shelfMask);
			_container.mask = _shelfMask;
			
			_resWrapper = new Sprite();
			_container.addChild(_resWrapper);
			
			_scroller = new Scroller(10, 10, 10, 10, _resWrapper, ScrollerType.VERTICAL);
			addChild(_scroller);
			
			this.addEventListener(MouseEvent.CLICK, OnClick);
		}
		
		private function RemoveRes():void{
			var nItem:ResItem;
			while(_resWrapper.numChildren > 0){
				nItem = _resWrapper.removeChildAt(0) as ResItem;
				nItem.Dispose();
			}
			// 资源列表与滑条位置归位
			_resWrapper.y = 0;
			_scroller.updateSliderPos();
		}
		
		/* Event Handler
		 * --------------------------------------------- */
		private function OnClick(evt:MouseEvent):void{
			if(evt.target is ResItem){	// 不冒泡，由父物体 "MPOnlineResBook" 直接处理
				if(_curResItem != null)_curResItem.isActive = false;
				var rItem:ResItem = evt.target as ResItem;
				rItem.isActive = true;
				_curResItem = rItem;
				dispatchEvent(new OnlineResEvent(PLAY_RES, false, false, rItem.vo));
			}
		}
		
		override public function SetLayout(areaWidth:int, areaHeight:int):void{
			super.SetLayout(areaWidth, areaHeight);
			
			var unitW:int = ResItem.WIDTH + 15;
			var unitY:int = ResItem.HEIGHT + 40;
			var limitW:int = Math.floor((areaWidth - _container.x - 40)/unitW) * unitW;	// 40是余留出来的
			var rItem:ResItem;
			var insertX:int = 0;
			var insertY:int = 0;
			
			_resWrapper.graphics.clear();
			_resWrapper.graphics.beginFill(0xfcfcf7, 0.7);
			_resWrapper.graphics.lineStyle(1, 0xc9c9c4, 1);
			_resWrapper.graphics.drawRoundRect(0,150, areaWidth, 25, 3, 3);
			
			for(var i:uint = 0; i < _resWrapper.numChildren; i++){
				
				rItem = _resWrapper.getChildAt(i) as ResItem;
				rItem.x = insertX;
				rItem.y = insertY;
				insertX += unitW;
				if(insertX >=limitW){
					insertX = 0;
					insertY += unitY;
					
					_resWrapper.graphics.drawRoundRect(0,insertY+150, areaWidth, 25, 3, 3);
				}
			}
			var shelfHeight:int = areaHeight - 50;
			_shelfMask.width = limitW;
			_shelfMask.height = shelfHeight;
			_shelfMask.x = _container.x = 30 + 30;
			_shelfMask.y = _container.y = int((areaHeight - shelfHeight)/2);
			
			_scroller.SetSize(shelfHeight, shelfHeight, _resWrapper.height);
			_scroller.x = areaWidth - 30;
			_scroller.y = int((areaHeight - shelfHeight)/2);
		}
		
		public function FillResWrapper(itemGroup:ItemGroup):void{
			RemoveRes();
			var olResVO:OnlineResVO;
			for (var i:uint = 0; i < itemGroup.Length; i++)
			{
				olResVO = itemGroup.Items[i];
				var rItem:ResItem = new ResItem(olResVO);	
				_resWrapper.addChild(rItem);
			}
			SetLayout(_areaW, _areaH);	// 得到数据后，手动重排内容
		}
		
		override public function Dispose():void{
			super.Dispose();
			this.removeEventListener(MouseEvent.CLICK, OnClick);
		}
	}
}